<?php 
	
	require_once('config.php');
	
	function GetBrandsByType($type_value)
	{
		$result = array();
		$brand_id_arr = explode('-',$type_value['type_brand']);
		foreach($brand_id_arr as $value)
		{
			$brand_id = (int)$value;
			//$brand_id = $value;
			$sql = mysql_query("SELECT * FROM `brand_tb` 
							WHERE `brand_tb`.`brand_id` = $brand_id", dbconnect());
			$result[] = mysql_fetch_assoc($sql);
		}
		return $result;
	}
	function GetBrandSearch($curPage, $rowPerPage, $keyword="")
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
						
		if($keyword != "")
			$sql = "SELECT * FROM `brand_tb` WHERE `brand_name` LIKE '%$keyword%' ORDER BY `brand_name` LIMIT $from , $rowPerPage";
		else
			$sql = "SELECT * FROM `brand_tb` ORDER BY `brand_name` LIMIT $from , $rowPerPage";
		/*var_dump($sql); die();*/
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($row = mysql_fetch_assoc($sql)) { 
			$result[] = $row;
		} 
		return $result; 
	}
	function GetBrandAll()
	{
		$sql = "SELECT * FROM `brand_tb` ORDER BY `brand_name`";
		/*var_dump($sql); die();*/
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($row = mysql_fetch_assoc($sql)) { 
			$result[] = $row;
		} 
		return $result; 
	}
	function GetNumBrandSearch($keyword="")
	{
		if($keyword != "")
			$sql = "SELECT count(brand_id) as num FROM `brand_tb` WHERE `brand_name` LIKE '%$keyword%'";
		else
			$sql = "SELECT count(brand_id) as num FROM `brand_tb` ";
		$sql = mysql_query($sql, dbconnect());
		
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	
	function InsertBrand($arr, $img_url)
	{
		$sql = "INSERT INTO `brand_tb` (`brand_name`, `brand_logo`, `brand_info_short`, `brand_info_full`) 
				VALUE ('{$arr['brand_name']}', '{$img_url}', '{$arr['brand_info_short']}', '{$arr['brand_info_full']}')";

		mysql_query($sql, dbconnect());
	}
	function GetBrandById($brand_id)
	{
		$sql = mysql_query("SELECT * FROM `brand_tb` 
							WHERE `brand_tb`.`brand_id`=$brand_id ", dbconnect());
		return mysql_fetch_assoc($sql) ;
	}
			
	function EditBrand($brand_id, $arr, $img_url)
	{
		// var_dump($arr); die();
		if ($img_url)
			$sql = "UPDATE `brand_tb` 
							SET `brand_name`='{$arr['brand_name']}', 
								`brand_logo` = '{$img_url}' ,
								`brand_info_short` = '{$arr['brand_info_short']}', 
								`brand_info_full` = '{$arr['brand_info_full']}'
								
							WHERE `brand_id`=$brand_id";
		else
			$sql = "UPDATE `brand_tb` 
							SET `brand_name`='{$arr['brand_name']}',
								`brand_info_short` = '{$arr['brand_info_short']}', 
								`brand_info_full` = '{$arr['brand_info_full']}'
							WHERE `brand_id`=$brand_id";
		
		mysql_query($sql, dbconnect());
	}
	function DeleteBrand($brand_id){
		return mysql_query("DELETE FROM `brand_tb` WHERE `brand_id`= $brand_id", dbconnect());
	}
?>
